package com.sx.micowell.module.homePage

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.SPUtils
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.response.getUrl
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragMineBinding
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.MineModel
import com.sx.micowell.module.MainActivity

/**
 * @Description 主页-我的
 * @Author 梁浩文
 * @Date 2021/7/27 9:16
 */
@Route(path = RoutePath.Mine)
class MineFragment : MyFragment<FragMineBinding, MainActivity>() {

    lateinit var viewModel: MineModel

    override fun initViewModel() {
        viewModel = getActivityScopeViewModel(MineModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_mine, BR.viewModel, viewModel)

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .init()
    }

    override fun initData() {
        viewModel.userInfo()
    }

    @SuppressLint("SetTextI18n", "UseCompatLoadingForDrawables")
    override fun initViewObservable() {
        super.initViewObservable()

        viewModel.loginSwitchEvent.observe(this, {
            RouteManager.getInstance().intentLogin(it)
            mActivity.finish()
        })

        // 刷新
        viewModel.ui.refreshEvent.observe(this, {
            binding.refreshLayout.finishRefresh(it)
        })

        // 数据更新
        viewModel.userInfoEvent.observe(this, { data ->
            // 图片
            if (!TextUtils.isEmpty(data.userPhoto)) {
                Glide.with(this).load(data.userPhoto.getUrl())
                    .apply(bitmapTransform( CircleCrop()))
                    .placeholder(R.drawable.icon_head_portrait)
                    .into(binding.layoutHead.imageHeadPortrait)
            }
            if (data.isVip) {
                binding.layoutHead.tvVipMsg.text = "有效期至 ${data.vipTime}"
                binding.layoutHead.layoutLook.text = "立即续费"
                binding.layoutHead.tvVip.text = "VIP会员"

                val drawable: Drawable = resources.getDrawable(R.drawable.ic_vip_tag, null)
                drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
                binding.layoutHead.tvVip.setCompoundDrawables(drawable, null, null, null)
            }
            // 昵称
            binding.layoutHead.tvUserName.text = data.nickname
            // 运动时间
            binding.layoutBillLading.tvMotionTime.text = data.duration+"分钟"
            // 总消耗
            binding.layoutBillLading.tvCalorie.text = data.calorie
            // 累计
            binding.layoutBillLading.tvRegisterCount.text = data.registerCount
            // 连续
            binding.layoutBillLading.tvRegister.text = data.register

            if (SPUtils.getInstance().getBoolean(SPKey.SP_UNREAD, true)) {
                binding.layoutHead.tvUnreadNum.visibility = View.VISIBLE
            } else {
                binding.layoutHead.tvUnreadNum.visibility = View.GONE
            }
        })
    }

    override fun onResume() {
        super.onResume()
        viewModel.userInfo()
    }
}